﻿using AutoMapper;
using EFEntity;
using ISerives;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serives
{
    public class major_changeServices : Imajor_changeServices
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;
        public major_changeServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }

    

        public async Task<List<major_change>> Fenye(FenYe fy)
        {
            SqlParameter[] ps =
            {
                new SqlParameter() { ParameterName="@pageSize",Value=fy.pageSize},
                new SqlParameter() { ParameterName="@keyName",Value="mch_id"},
                new SqlParameter() { ParameterName="@tabelName",Value="major_change"},
                new SqlParameter() { ParameterName="@where",Value=fy.where},
                new SqlParameter() { ParameterName="@currentPage",Value=fy.currentPage},
                new SqlParameter() { ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int},
                new SqlParameter() { ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int}
            };
            List<Emajor_change> list = await myDbContext.major_change.FromSqlRaw("exec proc_FenYe @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output, @pages output", ps).ToListAsync();
            fy.rows = (int)ps[5].Value;
            fy.pages = (int)ps[6].Value;
            List<major_change> list2 = new List<major_change>();

            foreach (Emajor_change t in list)
            {
                major_change ck = new major_change()
                {
                    mch_id = t.mch_id,
                    first_kind_id = t.first_kind_id,
                    first_kind_name = t.first_kind_name,
                    second_kind_id = t.second_kind_id,
                    second_kind_name = t.second_kind_name,
                    third_kind_id = t.third_kind_id,
                    third_kind_name = t.third_kind_name,
                    major_kind_id = t.major_kind_id,
                    major_kind_name = t.major_kind_name,
                    major_id = t.major_id,
                    major_name = t.major_name,
                    new_first_kind_id = t.new_first_kind_id,
                    new_first_kind_name = t.new_first_kind_name,
                    new_second_kind_id = t.new_second_kind_id,
                    new_second_kind_name = t.new_second_kind_name,
                    new_third_kind_id = t.new_third_kind_id,
                    new_third_kind_name = t.new_third_kind_name,
                    new_major_kind_id = t.new_major_kind_id,
                    new_major_kind_name = t.new_major_kind_name,
                    new_major_id = t.new_major_id,
                    new_major_name = t.new_major_name,
                    human_id = t.human_id,
                    human_name = t.human_name,
                    salary_standard_id = t.salary_standard_id,
                    salary_standard_name = t.salary_standard_name,
                    salary_sum = t.salary_sum,
                    new_salary_standard_id = t.new_salary_standard_id,
                    new_salary_standard_name = t.new_salary_standard_name,
                    new_salary_sum = t.new_salary_sum,
                    change_reason = t.change_reason,
                    check_reason = t.check_reason,
                    check_status = t.check_status,
                    register = t.register,
                    checker = t.checker,
                    regist_time = t.regist_time,
                    check_time = t.check_time,

                };
                list2.Add(ck);
            }
            return list2;
        }

        public async Task<List<major_change>> Select()
        {
            List<major_change> list1 = new List<major_change>();
            await Task.Run(() =>
            {
                List<Emajor_change> list = myDbContext.major_change.ToList();
                list1 = mapper.Map<List<major_change>>(list);

            });
            return list1; 
        }

        public async Task<Emajor_change> selectont(int id)
        {
            Emajor_change a = new Emajor_change();
            await Task.Run(() =>
            {
                a = myDbContext.major_change.Where(e => e.mch_id == id).FirstOrDefault();

            });
            return a;
        }

      
        public async Task<int> updeta(Emajor_change t)
        {
            Emajor_change es = myDbContext.major_change.Where(es => es.mch_id == t.mch_id).FirstOrDefault();
            es.new_first_kind_id = t.new_first_kind_id;
            es.new_first_kind_name = t.new_first_kind_name;
            es.new_second_kind_id = t.new_second_kind_id;
            es.new_second_kind_name = t.new_second_kind_name;
            es.new_third_kind_id = t.new_third_kind_id;
            es.new_third_kind_name = t.new_third_kind_name;
            es.new_major_kind_id = t.new_major_kind_id;
            es.new_major_kind_name = t.new_major_kind_name;
            es.new_major_id = t.new_major_id;
            es.new_major_name = t.new_major_name;
            es.new_salary_standard_id = t.new_major_id;
            es.new_salary_standard_name = t.new_major_name;
            es.new_salary_sum = t.new_salary_sum;
            es.change_reason = t.change_reason;
            es.check_status = 1;
            es.regist_time = t.regist_time;
            return await myDbContext.SaveChangesAsync();
        }
        public async Task<int> updetaqq(Emajor_change t)
        {
            Emajor_change es = myDbContext.major_change.Where(es => es.mch_id == t.mch_id).FirstOrDefault();
           
            es.check_reason = t.check_reason;
            es.checker = "啊giao";
            es.check_time = DateTime.Now;
            es.check_status = t.check_status;
            return await myDbContext.SaveChangesAsync();
        }


        }
}
